package toGoatLatin;

import java.util.HashSet;
import java.util.Set;

public class Solution {

    public String toGoatLatin(String sentence) {
        Set<Character> vowels = new HashSet<Character>() {{
            add('a');
            add('e');
            add('i');
            add('o');
            add('u');
            add('A');
            add('E');
            add('I');
            add('O');
            add('U');
        }};
        // 得到每个单词
        int i = 0; // 单词开头
        StringBuilder ans = new StringBuilder();
        StringBuilder a = new StringBuilder();
        a.append('a');
        while (i<sentence.length()) {
            int j = i;
            while (j<sentence.length()&&sentence.charAt(j)!=' ') {
                j++;
            }
            // 检查是不是元音字母
            if(vowels.contains(sentence.charAt(i))) {
                ans.append(sentence.substring(i,j)).append("ma").append(a).append(" ");
            } else {
                ans.append(sentence.substring(i+1,j)).append(sentence.charAt(i)).append("ma").append(a).append(" ");
            }
            a.append('a');
            i = j+1;
        }
        return ans.substring(0,ans.length()-1);
    }

}
